package com.volcreate.inn.controller;


import com.volcreate.inn.po.BlogLike;
import com.volcreate.inn.service.impl.BlogLikeService;
import com.volcreate.inn.service.impl.BlogService;
import com.volcreate.inn.service.impl.TagService;
import com.volcreate.inn.service.impl.TypeService;
import com.volcreate.inn.vo.BlogQuery;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zxc
 * @since 2020-08-23
 */
@Controller
public class BlogController {
    private static final String BLOGS = "blogs";
    private static final String BLOG = "blog";
    private static final String SEARCH = "search";

    private static final int MODE_UPDATE_TIME = 1;
    private static final int MODE_CREATE_TIME = 2;
    private static final int MODE_VIEWS = 3;

    private final BlogService blogService;
    private final TypeService typeService;
    private final TagService tagService;
    private final BlogLikeService blogLikeService;

    public BlogController(BlogService blogService, TypeService typeService, TagService tagService, BlogLikeService blogLikeService) {
        this.blogService = blogService;
        this.typeService = typeService;
        this.tagService = tagService;
        this.blogLikeService = blogLikeService;
    }


    @GetMapping(value = {"/blogs", "/search"})
    public String blogs(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                        Model model) {
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setSortBy("views");
        model.addAttribute("page", blogService.listByBlogQuery(page, 5, blogQuery));
        model.addAttribute("types", typeService.listByBlogSizeDesc(1, 6));
        model.addAttribute("tags", tagService.listByBlogSizeDesc(1, 9));
        blogQuery.setSortBy("gmt_modified");
        model.addAttribute("blogs", blogService.listByBlogQuery(1, 8, blogQuery));
        return BLOGS;
    }

    @PostMapping("/blogs")
    public String blogsPost(@RequestParam(value = "page", required = true, defaultValue = "1") int page,
                            Model model) {
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setSortBy("views");
        model.addAttribute("page", blogService.listByBlogQuery(page, 5, blogQuery));
        return "blogs :: blogsList";
    }


    @GetMapping("/blog/{id}")
    public String blog(@PathVariable Long id, Model model) {
        // 得到blog并将源为markdown的content内容转换为html页面
        model.addAttribute("blog", blogService.getAndConvert(id));
        return BLOG;
    }

    @PostMapping("/blogLike")
    @ResponseBody
    public String blogLikePost(@RequestParam(value = "like", required = true, defaultValue = "1") int like,
                               @RequestParam("id") Long blogId, HttpServletRequest request) {
        if (like == 1) {

            String ip4Addr = request.getRemoteAddr();
            BlogLike blogLike1 = blogLikeService.getByIp4(ip4Addr);
            if (blogLike1 == null) {
                BlogLike blogLike2 = new BlogLike();
                blogLike2.setIp4(ip4Addr);
                blogLikeService.save(blogLike2);
                blogService.updateLike(blogId);
                System.out.println(ip4Addr);
                return "OK";
            } else {
                long time = blogLike1.getLastTime().getTime();
                if (new Date().getTime() - 3_000 > time){
                    blogLike1.setLastTime(new Date());
                    blogLikeService.update(blogLike1,null);
                    blogService.updateLike(blogId);
                    return "OK";
                }else {
                    return "ERROR";
                }
            }
        }
        return "ERROR";
    }

    @PostMapping("/search")
    public String search(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                         @RequestParam String query,Model model) {
        System.out.println(query);

        model.addAttribute("page", blogService.listByBlogSearch(page, 5, query));
        model.addAttribute("types", typeService.listByBlogSizeDesc(1, 6));
        model.addAttribute("tags", tagService.listByBlogSizeDesc(1, 9));
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setSortBy("gmt_modified");
        model.addAttribute("blogs", blogService.listByBlogQuery(1, 8, blogQuery));
        return BLOGS;
    }

}

